package com.zy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.reggie.common.R;
import com.zy.reggie.dto.SetmealDto;
import com.zy.reggie.entity.Category;
import com.zy.reggie.entity.Setmeal;
import com.zy.reggie.service.CategoryService;
import com.zy.reggie.service.SetmealDishService;
import com.zy.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController{
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 保存套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}",setmealDto.toString());
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();//此时为空，因为mapper对应的泛型是SetMeal;

        //查询构造器
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper  = new LambdaQueryWrapper<>();
        //添加该过滤条件
        lambdaQueryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //此结果只包含了分类Id,并没有分类名字，所以分页构造器的泛型用SetmealDto
        setmealService.page(pageInfo,lambdaQueryWrapper);
        //上述操作进行分页查询
        //对setmealDtoPage进行赋值,忽略records字段，因为泛型不一样，得到的是setmeal,我们需要的是SetmealDto
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        //把records单独拿出来改造
        List<Setmeal> records = pageInfo.getRecords();
        //对records进行遍历
        List<SetmealDto> list = records.stream().map((item) ->{
            SetmealDto setmealDao = new SetmealDto();//此时为空，要进行拷贝
            BeanUtils.copyProperties(item,setmealDao);
            //首先根据id来查找对应的name;
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null) {
                String categoryName = category.getName();
                setmealDao.setCategoryName(categoryName);
            }
            return setmealDao;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);


        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("删除套餐的id是{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }
    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
